package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.TextColorBuilder;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.CourseCatalogueAdapter;
import cn.dujc.superkid.adapter.OnlineAnswerAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.AnswerListResult;
import cn.dujc.superkid.bean.MyAskResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.VideoListResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.utils.OnMultiClickListener;
import cn.dujc.superkid.utils.Preferences;
import cn.dujc.superkid.utils.ScrollEditText;

// TODO: 2018/8/21 0021 老师数据返回的josn是带<p>标签的数据，以后数据变动待做修改 
public class OnlineAnswerActivity extends BaseListActivity  {
    private List<AnswerListResult.AnswerEntity> followList = new ArrayList<>();
    private OnlineAnswerAdapter onlineAnswerAdapter;
    private EditText et_question;
    private TextView tv_sumit;
    private View mHeaderView;
    private int mPage = 0;
    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("在线答疑");
        getSwipeRefreshLayout().setEnabled(false);//禁止下拉刷新
    }

    private View initHeader() {
            mHeaderView = getLayoutInflater().inflate(R.layout.editview_item, null);
            et_question = mHeaderView.findViewById(R.id.et_question);
            mHeaderView.findViewById(R.id.tv_sumit).setOnClickListener(new OnMultiClickListener() {
                @Override
                public void onMultiClick(View v) {
                    setData();
                }
            });
        return mHeaderView;
    }
    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        onlineAnswerAdapter = new OnlineAnswerAdapter(followList);
        onlineAnswerAdapter.addHeaderView(initHeader());
        return onlineAnswerAdapter;
    }

    protected void recyclerViewOtherSetup() {
        if (mRvList != null) {
            //去掉下划线
//            mRvList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//            myQuestionAdapter.disableLoadMoreIfNotFullPage();//禁止加载
        }
    }

    @Override
    protected void onItemClick(int position) {

    }

    @Override
    protected void loadMore() {
        loadData();
    }

    @Override
    protected void reload() {
        showRefreshing();
        mPage = 0;
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }else{
            final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
            HttpHelper.getAnswerOnline().answer(QMap.build()
                    .put("user_id", loginResult.getUid())
                    .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                    .put("logincode", loginResult.getLogincode())
                    .put("num", Globals.PAGE_SIZE)
                    .put("page", mPage)
                    .create()).enqueue(new ResultCallBack<Result<AnswerListResult>>(mActivity) {
                @Override
                public void onSuccessCallback(int code, Result<AnswerListResult> body) {
                    if (mPage <= 0) followList.clear();
                    boolean done = false;
                    if (body != null){
                        final AnswerListResult data = body.getData();
                        if (data != null) {
                            final java.util.List<AnswerListResult.AnswerEntity> list = data.getList();
                            if (list != null) {
                                OnlineAnswerActivity.this.followList.addAll(list);
                            }
                            done = mPage >= data.getPage_num();
                        }
                    }

                    Handler handler = new Handler();
                    final boolean finalDone = done;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            notifyDataSetChanged(finalDone);
                            mPage++;
                        }
                    }, 500);

                }

                @Override
                public void onFailureCallback(int code, String msg) {
                    loadFailure();
                    loadDone(true,false);
                }
            });
        }

    }

    private void setData() {
        ToastUtil.showToast(mActivity,"正在提交中...");
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult
                (mActivity);
        //  public Call<Result<Object>> answerAdd(@FieldMap QMap map);
        HttpHelper.getAnswerOnline().answerAdd(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("question",et_question.getText().toString())
                .create()).enqueue(new ResultCallBack<Result<Object>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<Object> body) {
                ToastUtil.showToast(mActivity,body.getMsg());
                et_question.setText(null);
                reload();
            }
            @Override
            public void onFailureCallback(int code, String msg) {
                loadFailure();
                loadDone(true,false);
            }
        });
    }
}
